package com.resys.server.task;

import com.alibaba.fastjson.JSON;
import com.resys.entity.po.Meetingroom;
import com.resys.entity.po.Reservationorder;
import com.resys.entity.query.ReservationorderQuery;
import com.resys.service.ReservationorderService;
import com.resys.service.impl.MeetingroomServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.resys.server.websocket.WebSocketServer;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class OredrTask {
    @Resource
    private ReservationorderService reservationorderService;
    @Resource
    private MeetingroomServiceImpl meetingroomService;
    @Resource
    private WebSocketServer webSocketServer;
    @Scheduled(cron = "0 * * * * ?")
    public void processTimeoutOrder(){
        log.info("定时任务执行");
        ReservationorderQuery od=  new ReservationorderQuery();
        od.setStatus(0);
        List<Reservationorder> list = reservationorderService.findListByParam(od);
        for(Reservationorder order:list) {

            Meetingroom room = meetingroomService.getMeetingroomByRoomId(order.getRoomId());
            if (order.getLockedUntil().before(new Date())){
                order.setStatus(2);
                room.setStatus(0);
              reservationorderService.updateReservationorderByOrderId(order,order.getOrderId());
              meetingroomService.updateMeetingroomByRoomId(room,room.getRoomId());
            }
        }
        od.setStatus(1);
        List<Reservationorder> listavoke = reservationorderService.findListByParam(od);
        for(Reservationorder order:listavoke) {
            if (order.getStartTime().before(new Date())){
                Map map = new HashMap();
                map.put("customer", od.getOrderId());
                map.put("content","该订单开始使用");
                String json = JSON.toJSONString(map);
                webSocketServer.sendToAllClient(json);
            }

        }
    }
    @Scheduled(cron = "0 0 0 * *  ?")
    public void processComplteOrder(){

        ReservationorderQuery od=  new ReservationorderQuery();
        od.setStatus(1);
        List<Reservationorder> list = reservationorderService.findListByParam(od);
        for(Reservationorder order:list) {
            if (order.getEndTime().before(new Date())){
                order.setStatus(3);
                reservationorderService.updateReservationorderByOrderId(order,order.getOrderId());
            }

        }
    }



}


